import path = require('path')
import { FileUtil } from 'tools-mock'

const iconUrl = path.join(__dirname, '../../public/vicon')

/**
 * 对所有图标库进行统计
 */
const init = async () => {
    const iconMap = {} as any
    let num = 0

    const notFiles = ['DS_Store', '_keys.json', '_all.json', '_size.json']
    const isNot = (filename: string) => {
        for (let i = 0; i < notFiles.length; i++) {
            if (filename.indexOf(notFiles[i]) !== -1) return true
        }
        return false
    }
    await FileUtil.readDirectory(iconUrl, async (filename) => {
        if (isNot(filename) || !filename.endsWith('.json')) return

        let _sp = filename.split(iconUrl + '/')[1].split('/')
        const iconName = _sp[1].split('.json')[0]
        if (!iconMap[_sp[0]]) iconMap[_sp[0]] = {}
        iconMap[_sp[0]][iconName] = filename
        num++
    })

    const _keys = Object.keys(iconMap)

    for (let i = 0; i < _keys.length; i++) {
        const _obj = iconMap[_keys[i]]
        const _itemkeys = Object.keys(_obj)
        for (let j = 0; j < _itemkeys.length; j++) {
            const jname = _obj[_itemkeys[j]]
            _obj[_itemkeys[j]] = await FileUtil.readFile(jname)
        }
    }
    FileUtil.writeFile(path.join(iconUrl, '_keys.json'), JSON.stringify(Object.keys(iconMap)))
    FileUtil.writeFile(path.join(iconUrl, '_size.json'), JSON.stringify({ num }))

    Object.keys(iconMap).forEach((key) => {
        FileUtil.writeFile(path.join(iconUrl, key + '/_all.json'), JSON.stringify(iconMap[key]))
    })

    // console.log(iconMap)
}

init()
